package com.scm.service.impl;

import com.scm.common.core.domain.entity.SysUser;
import com.scm.common.enums.OrderOperationType;
import com.scm.common.utils.DateUtils;
import com.scm.converter.OrderConverter;
import com.scm.dto.req.order.OrderZhipaiReq;
import com.scm.persist.model.ScmOrderLog;
import com.scm.persist.model.ScmOrderOperation;
import com.scm.persist.model.ScmOrderOperationExample;
import com.scm.persist.mybatis.ScmOrderOperationMapper;
import com.scm.service.IScmOrderLogService;
import com.scm.service.IScmOrderOperationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * 订单主Service业务层处理
 * 
 * @author reptilemanager
 * @date 2024-02-08
 */
@Slf4j
@Service
public class ScmOrderOperationServiceImpl implements IScmOrderOperationService
{
    @Autowired
    ScmOrderOperationMapper orderOperationMapper;

    @Autowired
    private IScmOrderLogService orderLogService;

    @Override
    public boolean zhipaiOrder(OrderZhipaiReq req, SysUser loginSysUser ) {

        ScmOrderOperationExample example = new ScmOrderOperationExample();
        example.createCriteria().andScmOrderIdEqualTo(req.getOrderId());
        List<ScmOrderOperation> list = orderOperationMapper.selectByExample(example);
        if(CollectionUtils.isEmpty(list)){
            return false;
        }else{

            ScmOrderOperation orderOperation = list.get(0);
            orderOperation.setZhipairen(String.valueOf(req.getZhipaiUserId()));
            orderOperation.setZhipaiTime(System.currentTimeMillis());
            orderOperation.setZhipaiTimeStr(DateUtils.getTime());
            orderOperation.setZhipaiOperator(String.valueOf(req.getZhipaiOperator()));

            orderOperationMapper.updateByPrimaryKey(orderOperation);

            ScmOrderLog orderLog = OrderConverter.createOrderLog(req.getOrderId(), OrderOperationType.ZhiPai,loginSysUser,loginSysUser.getLoginName()+"完成了指派订单（"+req.getOrderId()+"）操作");
            orderLogService.saveOrderLog(orderLog);

        }

        return false;
    }

    @Override
    public void updateOrderOperationByOrderId(ScmOrderOperation orderOperation) {

        ScmOrderOperationExample example = new ScmOrderOperationExample();
        example.createCriteria().andScmOrderIdEqualTo(orderOperation.getScmOrderId());
        orderOperationMapper.updateByExampleSelective(orderOperation,example);

    }
}
